<?php
/* $Id$ */
/*
	system.php
	part of m0n0wall (http://m0n0.ch/wall)

	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

##|+PRIV
##|*IDENT=page-system-generalsetup
##|*NAME=System: General Setup page
##|*DESCR=Allow access to the 'System: General Setup' page.
##|*MATCH=system.php*
##|-PRIV


require("guiconfig.inc");

$pconfig['hostname'] = $config['system']['hostname'];
$pconfig['domain'] = $config['system']['domain'];
list($pconfig['dns1'],$pconfig['dns2'],$pconfig['dns3'],$pconfig['dns4']) = $config['system']['dnsserver'];

$pconfig['dns1gwint'] = $config['system']['dns1gwint'];
$pconfig['dns2gwint'] = $config['system']['dns2gwint'];
$pconfig['dns3gwint'] = $config['system']['dns3gwint'];
$pconfig['dns4gwint'] = $config['system']['dns4gwint'];

$pconfig['dnsallowoverride'] = isset($config['system']['dnsallowoverride']);
$pconfig['timezone'] = $config['system']['timezone'];
$pconfig['timeupdateinterval'] = $config['system']['time-update-interval'];
$pconfig['timeservers'] = $config['system']['timeservers'];
$pconfig['theme'] = $config['system']['theme'];

if(isset($config['system']['enableremoteconfiguration']))
    $pconfig['enableremoteconfiguration'] = true;

if (!isset($pconfig['timeupdateinterval']))
	$pconfig['timeupdateinterval'] = 300;
if (!$pconfig['timezone'])
	$pconfig['timezone'] = "Etc/UTC";
if (!$pconfig['timeservers'])
	$pconfig['timeservers'] = "pool.ntp.org";

$changedesc = "System: ";
$changecount = 0;

function is_timezone($elt) {
	return !preg_match("/\/$/", $elt);
}

if($pconfig['timezone'] <> $_POST['timezone']) {
	/* restart firewall log dumper helper */
	require_once("functions.inc");
	$pid = `ps awwwux | grep -v "grep" | grep "tcpdump -v -l -n -e -ttt -i pflog0"  | awk '{ print $2 }'`;
	if($pid) {
		mwexec("kill $pid");
		usleep(1000);
	}		
	filter_pflog_start();
}

exec('/usr/bin/tar -tzf /usr/local/stairway/data/zoneinfo.tgz', $timezonelist);
$timezonelist = array_filter($timezonelist, 'is_timezone');
sort($timezonelist);

$multiwan = false;
//$interfaces = get_configured_interface_list();
//foreach($interfaces as $interface) {
//	if(interface_has_gateway($interface)) {
//		$multiwan = true;
//	}
//}

if ($_POST) {

	$changecount++;
	
	unset($input_errors);
	$pconfig = $_POST;

	/* input validation */
	$reqdfields = split(" ", "hostname domain");
	$reqdfieldsn = split(",", "主机名,域");

	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);

	if ($_POST['hostname'] && !is_hostname($_POST['hostname'])) {
		$input_errors[] = "主机名只能由下列字符组成：a-z，0-9和'-'。";
	}
	if ($_POST['domain'] && !is_domain($_POST['domain'])) {
		$input_errors[] = "域名只能由下列字符组成：a-z，0-9，'-'和'.'。";
	}
	if (($_POST['dns1'] && !is_ipaddr($_POST['dns1'])) || ($_POST['dns2'] && !is_ipaddr($_POST['dns2']))) {
		$input_errors[] = "请输入有效的IP地址作为DNS服务器。";
	}
	if (($_POST['dns3'] && !is_ipaddr($_POST['dns3'])) || ($_POST['dns4'] && !is_ipaddr($_POST['dns4']))) {
		$input_errors[] = "请输入有效的IP地址作为DNS服务器。";
	}	
	if ($_POST['webguiport'] && (!is_numericint($_POST['webguiport']) ||
			($_POST['webguiport'] < 1) || ($_POST['webguiport'] > 65535))) {
		$input_errors[] = "请输入有效的端口作为web配置界面的访问端口。";
	}

	$direct_networks_list = explode(" ", get_direct_networks_list());
	for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
		$dnsitem = "dns{$dnscounter}";
		$dnsgwitem = "dns{$dnscounter}gwint";
		if ($_POST[$dnsgwitem]) {
			if(interface_has_gateway($_POST[$dnsgwitem])) {
				foreach($direct_networks_list as $direct_network) {
					if(ip_in_subnet($_POST[$dnsitem], $direct_network)) {
						$input_errors[] = "You can not assign a gateway to DNS '{$_POST[$dnsitem]}' server which is on a directly connected network.";
					}
				}
			}
		}
	}

	$t = (int)$_POST['timeupdateinterval'];
	if (($t < 0) || (($t > 0) && ($t < 6)) || ($t > 1440)) {
		$input_errors[] = "时间同步间隔必须是0（禁用）或者6-1440之间。";
	}
	foreach (explode(' ', $_POST['timeservers']) as $ts) {
		if (!is_domain($ts)) {
			$input_errors[] = "NTP时间服务器只能由下列字符组成：a-z，0-9，'-'和'.'。";
		}
	}

	if (!$input_errors) {
		update_if_changed("hostname", $config['system']['hostname'], strtolower($_POST['hostname']));
		update_if_changed("domain", $config['system']['domain'], strtolower($_POST['domain']));

		update_if_changed("timezone", $config['system']['timezone'], $_POST['timezone']);
		update_if_changed("NTP servers", $config['system']['timeservers'], strtolower($_POST['timeservers']));
		update_if_changed("NTP update interval", $config['system']['time-update-interval'], $_POST['timeupdateinterval']);

		/* pfSense themes */
		update_if_changed("System Theme", $config['theme'], $_POST['theme']);

		/* XXX - billm: these still need updating after figuring out how to check if they actually changed */
		unset($config['system']['dnsserver']);
		if ($_POST['dns1'])
			$config['system']['dnsserver'][] = $_POST['dns1'];
		if ($_POST['dns2'])
			$config['system']['dnsserver'][] = $_POST['dns2'];
		if ($_POST['dns3'])
			$config['system']['dnsserver'][] = $_POST['dns3'];
		if ($_POST['dns4'])
			$config['system']['dnsserver'][] = $_POST['dns4'];

		$olddnsallowoverride = $config['system']['dnsallowoverride'];

		unset($config['system']['dnsallowoverride']);
		$config['system']['dnsallowoverride'] = $_POST['dnsallowoverride'] ? true : false;

		/* which interface should the dns servers resolve through? */
		if($_POST['dns1gwint']) 
			$config['system']['dns1gwint'] = $pconfig['dns1gwint'];
		else 
			unset($config['system']['dns1gwint']);

		if($_POST['dns2gwint']) 
			$config['system']['dns2gwint'] = $pconfig['dns2gwint'];
		else
			unset($config['system']['dns2gwint']);

		if($_POST['dns3gwint']) 
			$config['system']['dns3gwint'] = $pconfig['dns3gwint'];
		else 
			unset($config['system']['dns3gwint']);

		if($_POST['dns4gwint']) 
			$config['system']['dns4gwint'] = $pconfig['dns4gwint'];
		else
			unset($config['system']['dns4gwint']);

        if($_POST['enableremoteconfiguration'] == 'yes') 
            $config['system']['enableremoteconfiguration'] = true;
        else
            unset($config['system']['enableremoteconfiguration']);
			
			
		if ($changecount > 0)
			write_config($changedesc);

		$retval = 0;
		config_lock();
		$retval = system_hostname_configure();
		$retval |= system_hosts_generate();
		$retval |= system_resolvconf_generate();
		$retval |= services_dnsmasq_configure();
		$retval |= system_timezone_configure();
		$retval |= system_ntp_configure();

		/* XXX: ermal -- What is this supposed to do?! */
		if ($olddnsallowoverride != $config['system']['dnsallowoverride'])
			$retval |= interface_configure();

		// Reload the filter - plugins might need to be run.
		filter_configure();
		
		config_unlock();

		$savemsg = get_std_save_message($retval);
	}
}

$pgtitle = array("系统信息","一般设置");
include("head.inc");

?>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php
	include("fbegin.inc");
	if ($input_errors)
		print_input_errors($input_errors);
	if ($savemsg)
		print_info_box($savemsg);
?>
<div class="soft_cont_right_table">
	<form action="general_setup.php" method="post">
		
		<table width="100%" border="0" cellpadding="6" cellspacing="0">
			<tr>
				<td colspan="2" valign="top" class="listtopic">一般设置</td>
			</tr>
			<tr>
				<td width="22%" valign="top" class="vncellreq">主机名</td>
				<td width="78%" class="vtable"> <input name="hostname" type="text" class="formfld unknown" id="hostname" size="40" value="<?=htmlspecialchars($pconfig['hostname']);?>">
					<br/>
					<span class="vexpl">
						stairway所在主机的名称，不包括域。
						<br>
						例如 <em>firewall</em>
					</span>
				</td>
			</tr>
			<tr>
				<td width="22%" valign="top" class="vncellreq">域</td>
				<td width="78%" class="vtable"> <input name="domain" type="text" class="formfld unknown" id="domain" size="40" value="<?=htmlspecialchars($pconfig['domain']);?>">
					<br/>
					<span class="vexpl">
						例如 <em>mycorp.com</em>
					</span>
				</td>
			</tr>
			<tr>
				<td width="22%" valign="top" class="vncell">DNS服务器</td>
				<td width="78%" class="vtable">
					<p>
						<table>
							<tr>
								<td><b>DNS服务器</b></td>
								<?php if ($multiwan): ?>
								<td><b>Use gateway</b></td>
								<?php endif; ?>
							</tr>
							<?php
								for ($dnscounter=1; $dnscounter<5; $dnscounter++):
									$fldname="dns{$dnscounter}gwint";
							?>
							<tr>
								<td>
									<input name="dns<?php echo $dnscounter;?>" type="text" class="formfld unknown" id="dns<?php echo $dnscounter;?>" size="20" value="<?php echo $pconfig['dns'.$dnscounter];?>">
								</td>
								<td>
<?php if ($multiwan): ?>
									<select name='<?=$fldname;?>'>
										<?php
											$interface = "none";
											$dnsgw = "dns{$dnscounter}gwint";
											if($pconfig[$dnsgw] == $interface) {
												$selected = "selected";
											} else {
												$selected = "";
											}
											echo "<option value='$interface' $selected>". ucwords($interface) ."</option>\n";
											foreach($interfaces as $interface) {
												if(interface_has_gateway($interface)) {
													if($pconfig[$dnsgw] == $interface) {
														$selected = "selected";
													} else {
														$selected = "";
													}
													$friendly_interface = convert_friendly_interface_to_friendly_descr($interface);
													echo "<option value='$interface' $selected>". ucwords($friendly_interface) ."</option>\n";
												}
											}
										?>
									</select>
<?php endif; ?>
								</td>
							</tr>
							<?php endfor; ?>
						</table>
						<br>
						<span class="vexpl">
							DNS服务器的IP地址， 这些设置同样会被DHCP服务，DNS转发和PPTP VPN客户使用。
							<br/>
							<?php if($multiwan): ?>
							<br/>
							In addition, select the gateway for each DNS server.
							You should have a unique DNS server per gateway.
							<br/>
							<?php endif; ?>
							<br/>
							<input name="dnsallowoverride" type="checkbox" id="dnsallowoverride" value="yes" <?php if ($pconfig['dnsallowoverride']) echo "checked"; ?>>
							<strong>
							    允许DNS服务器列表被DHCP/PPP服务改写。
							</strong>
							<br/>
							如果选中该选项，<?=$g['product_name'];?>会尽可能使用DHCP/PPP服
							务器下发的DNS服务器。DHCP和PPTP VPN客户也不会得到上述DNS服务器。
						</span>
					</p>
				</td>
			</tr>
			<tr>
				<td width="22%" valign="top" class="vncell">时区</td>
				<td width="78%" class="vtable">
					<select name="timezone" id="timezone">
						<?php foreach ($timezonelist as $value): ?>
						<option value="<?=htmlspecialchars($value);?>" <?php if ($value == $pconfig['timezone']) echo "selected"; ?>>
							<?=htmlspecialchars($value);?>
						</option>
						<?php endforeach; ?>
					</select>
					<br/>
					<span class="vexpl">
						选择您的地区
					</span>
				</td>
			</tr>
<!--
			<tr>
				<td width="22%" valign="top" class="vncell">Time update interval</td>
				<td width="78%" class="vtable">
					<input name="timeupdateinterval" type="text" class="formfld unknown" id="timeupdateinterval" size="4" value="<?=htmlspecialchars($pconfig['timeupdateinterval']);?>">
					<br/>
					<span class="vexpl">
						Minutes between network time sync. 300 recommended,
						or 0 to disable
					</span>
				</td>
			</tr>
-->
			<tr>
				<td width="22%" valign="top" class="vncell">NTP时间服务器</td>
				<td width="78%" class="vtable">
					<input name="timeservers" type="text" class="formfld unknown" id="timeservers" size="40" value="<?=htmlspecialchars($pconfig['timeservers']);?>">
					<br/>
					<span class="vexpl">
					    用空格分隔多个时间服务器
					</span>
				</td>
			</tr>
            <tr>
                <td width="22%" valign="top" class="vncell">远程管理</td>
                <td width="78%" class="vtable">
                    <input name="enableremoteconfiguration" type="checkbox" id="enableremoteconfiguration" size="4" value="yes" <?php if(isset($pconfig['enableremoteconfiguration'])) echo 'checked' ?>> 允许通过WAN访问WEB管理界面和SSH。
                </td>
            </tr>			
			<tr>
				<td colspan="2" class="list" height="12">&nbsp;</td>
			</tr>
			<!--
			<tr>
				<td colspan="2" valign="top" class="listtopic">Theme</td>
			</tr>
			<tr>
				<td width="22%" valign="top" class="vncell">&nbsp;</td>
				<td width="78%" class="vtable">
					<select name="theme">
						<?php
							$files = return_dir_as_array("/usr/local/stairway/www/themes/");
							foreach($files as $f):
								if ((substr($f, 0, 1) == "_") && !isset($config['system']['developer']))
									continue;
								if ($f == "CVS")
									continue;
								$curtheme = "pfsense";
								if ($config['theme'])
									$curtheme = $config['theme'];
								$selected = "";
								if($f == $curtheme)
									$selected = " SELECTED";
						?>
						<option <?=$selected;?>><?=$f;?></option>
						<?php endforeach; ?>
					</select>
					<strong>
						This will change the look and feel of
						<?=$g['product_name'];?>.
					</strong>
				</td>
			</tr>
			-->
			<tr>
				<td colspan="2" class="list" height="12">&nbsp;</td>
			</tr>			
			<tr>
				<td width="22%" valign="top">&nbsp;</td>
				<td width="78%">
					<input name="Submit" type="submit" class="formbtn" value="保存">
				</td>
			</tr>
		</table>
	</form>
</div>
<?php include("fend.inc"); ?>
</body>
</html>
